package com.open.party.cloud.view.login

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import androidx.appcompat.app.AppCompatActivity
import com.open.party.cloud.MainActivity
import com.open.party.cloud.MainApplication
import com.open.party.cloud.R
import com.sinothk.android.utils.XUtils
import com.sinothk.permission.PermissionManager
import com.sinothk.permission.PermissionResultListener
import com.sinothk.permission.lib.PermissionsUtil
import com.xinstall.XInstall
import com.xinstall.listener.XWakeUpAdapter
import com.xinstall.model.XAppData


class WelcomeActivity : AppCompatActivity() {

    private fun checkPermissions() {
        val tip = PermissionsUtil.TipInfo("授权提示", "获取必须的手机权限不全！", "放弃使用", "授权")
        if (PermissionManager.haveAllPermission(this, MainApplication.permissions)) {
            timer.start()
        } else {
            PermissionManager.requestAllPermission(this, object : PermissionResultListener {
                override fun permissionFail(p0: Array<out String>?) {
                    XUtils.toast().show("放弃使用")
                    finish()
                }

                override fun permissionSuccess(p0: Array<out String>?) {
                    checkPermissions()
                }
            }, MainApplication.permissions, true, tip)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        checkPermissions()

        //获取唤醒参数
        XInstall.getWakeUpParam(getIntent(), wakeUpAdapter);
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        // 此处要调用，否则App在后台运行时，会无法截获
        XInstall.getWakeUpParam(getIntent(), wakeUpAdapter)
    }

    var wakeUpAdapter: XWakeUpAdapter = object : XWakeUpAdapter() {
        override fun onWakeUp(XAppData: XAppData) {
            //获取渠道数据
            val channelCode = XAppData.channelCode

            //获取数据
            val data = XAppData.extraData
            //通过链接后面携带的参数或者通过webSdk初始化传入的data值。
            val uo = data["uo"]
            //webSdk初始，在buttonId里面定义的按钮点击携带数据
            val co = data["co"]

            //获取时间戳
            val timeSpan = XAppData.timeSpan
        }
    }

    override fun onDestroy() {
        super.onDestroy()
//        wakeUpAdapter = null
    }

    @SuppressLint("SetTextI18n")
    var timer: CountDownTimer = object : CountDownTimer(2000, 1000) {

        override fun onTick(millisUntilFinished: Long) {
//            timerTv.text = "${millisUntilFinished / 1000}s"
        }

        override fun onFinish() {
//            timerTv.text = "0s"

            XUtils.intent().openActivity(this@WelcomeActivity, MainActivity::class.java)
                .finish(true).start()

//            if (LocalCache.getAutoLogin()) {
//                XUtils.intent().openActivity(this@WelcomeActivity, MainActivity::class.java)
//                    .finish(true).start()
//            } else {
//                XUtils.intent().openActivity(this@WelcomeActivity, LoginActivity::class.java)
//                    .finish(true).start()
//            }
        }
    }
}
